﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class Login : System.Web.UI.Page
    {
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //ViewState["retu"] = Request.UrlReferrer.ToString();
            }
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }

        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            string userName = UnameTextBox.Text.Trim().ToUpper();
            UserController userController=new UserController();
            int result = userController.CheckUserExsiting(userName);
            if (result == 1)
            {
                UserEntity user = userController.FindUserByUserName(userName);
                string password = PWTextBox.Text.Trim();
                string enPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
                int loginResult=userController.CheckLogin(userName, enPassword);
                if(loginResult==1)
                {
                    Session["User"] = user;
                    Response.Redirect("Default.aspx?target=home");
                }
                if(loginResult==0)
                {
                    //msgLablel.Text = "User name and password doesn't match!";
                    Session["FlashMessage"] = "Error: User Name And Password Doesn't Match!";
                    Session["FlashMessageType"] = "error";
                }
            }
            if(result==0)
            {
               // msgLablel.Text = "No such user!";
                Session["FlashMessage"] = "Error: No Such User!";
                Session["FlashMessageType"] = "error";
            }

        }
    }
}